<?php

class AjaxController extends Controller {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to access 'index' and 'view' actions.
                'actions' => array('ajax'),
                'users' => array('*'),
            ),
        );
    }

    public function actionAjax() {
        $apitype = $_GET['apitype']; // get获取接口类型
        $mod = $_GET['mod'];  //获取接口名称

        $api = new Api($apitype); // 初始化接口类

        if ($mod == 'userLogon') {
            $apiurl = "/LdAccountServer/userAccount/" . $mod;
        } elseif ($mod == 'updatePwdByPhone') {
            $apiurl = "/LdAccountServer/userAccount/" . $mod;
            $params = array(
                'UpdatePwdByPhoneReq' => array(
                    'phone' => $_POST['GetpwdForm']['mobile']
                )
            );
            $row = $api->apiclient($apiurl, $params);
        } elseif ($mod == 'addUserContactInfo') {
            if (Yii::app()->request->isAjaxRequest) {
                $value = Yii::app()->request->getParam("HomeForm");
                $apiurl = "/LdAccountServer/order/" . $mod;
                $params = array(
                    'AddUserContactInfoReq' => array(
                        'userId' => Yii::app()->user->id,
                        'province' => trim($value['province']),
                        'city' => trim($value['city']),
                        'area' => trim($value['area']),
                        'street' => trim($value['street']),
                        'streetDetail' => trim($value['streetDetail']),
                        'postalcode' => trim($value['postalcode']),
                        'recipients' => trim($value['recipients']),
                        'mobile' => trim($value['mobile']),
                        'telephone' => trim($value['extension']) ? trim($value['areacode']) . '-' . trim($value['phonenum']) . '转' . trim($value['extension']) : trim($value['areacode']) . '-' . trim($value['phonenum']),
                        'defaultAddress' => trim($value['default']),
                    )
                );
                if (Yii::app()->user->id) {
                    if (!trim($value['province']) || !trim($value['city']) || !trim($value['area']) || !trim($value['street'])) {
                        $status['code'] = 0;
                        $status['msg'] = '区域/地址不能为空';
                    } elseif (!trim($value['streetDetail'])) {
                        $status['code'] = 0;
                        $status['msg'] = '详细地址不能为空';
                    } elseif (!trim($value['postalcode'])) {
                        $status['code'] = 0;
                        $status['msg'] = '邮编不能为空';
                    } elseif (!trim($value['recipients'])) {
                        $status['code'] = 0;
                        $status['msg'] = '收件人不能为空';
                    } elseif (!trim($value['mobile'])) {
                        $status['code'] = 0;
                        $status['msg'] = '手机号不能为空';
                    } else {
                        $status['code'] = 1;
                        $status['msg'] = trim($value['default']);
                        $row = $api->apiclient($apiurl, $params);
                    }
                } else {
                    $status['code'] = -1;
                }
            }
        } elseif ($mod == 'getContactInfo') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/order/" . $mod;
                $params = array(
                    'GetContactInfoReq' => array(
                        'id' => Yii::app()->user->id
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'getUserContactInfo') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/order/" . $mod;
                $params = array(
                    'GetUserContactInfoReq' => array(
                        'userId' => Yii::app()->user->id
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'delContactInfo') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/order/" . $mod;
                $addressid = $_POST['addressid'];
                $params = array(
                    'DelContactInfoReq' => array(
                        'id' => $addressid,
                    )
                );
                if (Yii::app()->user->id) {
                    if ($addressid) {
                        $status['code'] = 1;
                        $row = $api->apiclient($apiurl, $params);
                    } else {
                        $status['code'] = 0;
                        $status['msg'] = '收货地址信息不存在';
                    }
                } else {
                    $status['code'] = -1;
                }
            }
        } elseif ($mod == 'addUserCart') {
            if (Yii::app()->request->isAjaxRequest) {
                $value = Yii::app()->request->getParam("ProductForm");
                $apiurl = "/LdAccountServer/order/" . $mod;
                $params = array(
                    'AddUserCartReq' => array(
                        'userId' => Yii::app()->user->id,
                        'sellerId' => $value['sellerid'],
                        'productSkuId' => $value['skuid'],
                        'amount' => $value['skuquantity']
                    )
                );
                if (Yii::app()->user->id) {
                    if ($value['sellerid'] <= 0) {
                        $status['code'] = 0;
                        $status['msg'] = '卖家不存在';
                    } elseif ($value['skuid'] <= 0) {
                        $status['code'] = 0;
                        $status['msg'] = '请选择商品分类';
                    } elseif ($value['skuquantity'] <= 0) {
                        $status['code'] = 0;
                        $status['msg'] = '购买数量不能为0';
                    } else {
                        $status['code'] = 1;
                        $row = $api->apiclient($apiurl, $params);
                    }
                } else {
                    $status['code'] = -1;
                }
            }
        } elseif ($mod == 'addBuyerCoupons') {
            if (Yii::app()->request->isAjaxRequest) {
//                $value = Yii::app()->request->getParam("ProductForm");
                $apiurl = "/LdAccountServer/userAccount/" . $mod;
                $params = array(
                    'AddBuyerCouponsReq' => array(
                        'buyerId' => Yii::app()->user->id,
                        'modelId' => 2
                    )
                );
                
                $apiurl_chk = "/LdAccountServer/userAccount/getBuyerCoupons";
                $params_chk = array(
                    'GetBuyerCouponsReq' => array(
                        'buyerId' => Yii::app()->user->id,
                        'modelId' => 2
                    )
                );
                $row_chk = $api->apiclient($apiurl_chk, $params_chk);
                
                if (Yii::app()->user->id) {
                    if (count($row_chk['GetBuyerCouponsListRsp']['list']) > 0) {
                        $status['code'] = 0;
                        $status['msg'] = '红包已领取，每人只能领取一个红包哦';
                    }else {
                        $status['code'] = 1;
                        $row = $api->apiclient($apiurl, $params);
                    }
                } else {
                    $status['code'] = -1;
                }
            }
        } elseif ($mod == 'delUserCart') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/order/" . $mod;
                $cartid = $_POST['cartid'];
                $params = array(
                    'DelUserCartReq' => array(
                        'id' => $cartid,
                    )
                );
                
                if (Yii::app()->user->id) {
                    if ($cartid) {
                        $status['code'] = 1;
                        $row = $api->apiclient($apiurl, $params);
                    } else {
                        $status['code'] = 0;
                        $status['msg'] = '购物车信息不存在';
                    }
                } else {
                    $status['code'] = -1;
                }
            }
        } elseif ($mod == 'getUserCart') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/order/" . $mod;
                $params = array(
                    'GetUserCartReq' => array(
                        'userId' => Yii::app()->user->id
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'updateUserCart') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/order/" . $mod;
                $id = $_GET['id'];
                $amound = $_GET['amound'];
                $params = array(
                    'UpdateUserCartReq' => array(
                        'id' => $id,
                        'amount' => $amound
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'getBusinessZone') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/userAccount/" . $mod;
                $params = array(
                    'GetAllBusinessZoneReq' => array(
                        'id' => '0',
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'getZone') {
            $id = !$_GET['id'] ? '1' : $_GET['id'];
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/userAccount/getBusinessZone";
                $params = array(
                    'GetAllBusinessZoneReq' => array(
                        'id' => $id,
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'setZone') {
            $id = $_GET['id'];
            $cookie = new CHttpCookie('zone', $id);
            $cookie->expire = time() + 60 * 60 * 24 * 30;  //有限期30天
            Yii::app()->request->cookies['zone'] = $cookie;
            $this->redirect(array('site/index'));
        } elseif ($mod == 'addLiulan') {
            $info = new stdClass();
            $info->id = $_GET['id'];
            $info->name = $_GET['name'];
            if(empty($info->id) || empty($info->name)){ 
                echo json_encode(array('content' => null, 'status' => 400));
                exit;
            }

            $cookie = Yii::app()->request->getCookies();
            $liulancookie = (array)json_decode($cookie['liulan']);
            
            // var_dump(json_encode($liulancookie));
            // exit;

            foreach ($liulancookie as $key => $value) {
                if($info->id == $value->id){ 
                    //已经存在
                    echo json_encode(array('content' => null, 'status' => 400));
                    exit;
                }
            }

            $liulancookie[] = $info;
            if(count($liulancookie) > 3){ 
                //删除最早存入的
                array_shift($liulancookie);
            }
            //var_dump($liulancookie);
            $cookie = new CHttpCookie('liulan', json_encode($liulancookie));
            $cookie->expire = time() + 60 * 60 * 24 * 7;  //有限期30天
            Yii::app()->request->cookies['liulan'] = $cookie;
            $new_li="";
            foreach ($liulancookie as $key => $value) {
                
                $new_li.='<li class="cl">';
                            $new_li.='<div class="z name tl">';
                               $new_li.='<a href="'.Yii::app()->createUrl("site/product", array("id" => $value->id)).'">'.$value->name.'</a>';
                            $new_li.='</div>';
                            $new_li.='<div class="btn" style="float:right;margin-right:10px;">';
                                $new_li.='<a href="javascript:void(0);" class="cmicons delliulan" liulanid="'.$value->id.'"></a>';
                            $new_li.='</div>';
                        $new_li.='</li>';
            }
            echo json_encode(array('content' => $new_li, 'status' => 200));
            exit;
        } elseif ($mod == 'getLiulan') {
            $cookie = Yii::app()->request->getCookies();
            $liulancookie = (array)json_decode($cookie['liulan']);
            $new_li="";
            foreach ($liulancookie as $key => $value) {
                
                $new_li.='<li class="cl">';
                            $new_li.='<div class="z name tl">';
                               $new_li.='<a href="'.Yii::app()->createUrl("site/product", array("id" => $value->id)).'">'.$value->name.'</a>';
                            $new_li.='</div>';
                            $new_li.='<div class="btn" style="float:right;margin-right:10px;">';
                                $new_li.='<a href="javascript:void(0);" class="cmicons delliulan" liulanid="'.$value->id.'"></a>';
                            $new_li.='</div>';
                        $new_li.='</li>';
            }
            echo json_encode(array('content' => $new_li, 'status' => 200));
            exit;

        } elseif ($mod == 'delLiulan') {
            $info = new stdClass();
            $info->id = $_GET['id'];

            $cookie = Yii::app()->request->getCookies();
            $liulancookie = json_decode($cookie['liulan']);

            foreach ($liulancookie as $key => $value) {
                if($info->id == $value->id){ 
                    if(count($liulancookie) == 1){ 
                        unset($cookie['liulan']);
                    }
                    unset($liulancookie[$key]);
                }else{ 
                    $liulancookie[$key] = $value;
                }
            }

           $cookie = new CHttpCookie('liulan', json_encode($liulancookie));
            $cookie->expire = time() + 60 * 60 * 24 * 7;  //有限期30天
            Yii::app()->request->cookies['liulan'] = $cookie;
        } elseif ($mod == 'getHeadImg') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/userAccount/" . $mod;
                $userid = Yii::app()->user->id;
                $params = array(
                    'GetHeadImgReq' => array(
                        'userId' => $userid,
                        'type' => ''
                    )
                );

                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'getHeadImgByType') {
            if (Yii::app()->request->isAjaxRequest) {
                $apiurl = "/LdAccountServer/userAccount/getHeadImg";
                $userid = Yii::app()->user->id;
                $params = array(
                    'GetHeadImgReq' => array(
                        'userId' => $userid,
                        'type' => '1'
                    )
                );

                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'getAddressList') {
            if (Yii::app()->request->isAjaxRequest) {
                $type = $_POST["type"];
                $parentId = $_POST["parentId"];
                $apiurl = "/LdAccountServer/order/" . $mod;
                $params = array(
                    'GetAddressListReq' => array(
                        'type' => $type,
                        'parentId' => $parentId
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif ($mod == 'closeOrder') {
            if (Yii::app()->request->isAjaxRequest) {
                $auth = urldecode(base64_decode($_GET['id']));
                $autharr = explode('|', $auth);
                $row = $autharr['1'];
            }
        } elseif($mod == 'closeOrderDone'){
            if (Yii::app()->request->isAjaxRequest) {
                $value = Yii::app()->request->getParam("OrderForm");
                $apiurl = "/LdAccountServer/order/updateOrderState";
                $params = array(
                    'UpdateOrderStateReq' => array(
                        'id' => $value['orderid'],
                        'orderState' => '0'
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        } elseif($mod == 'setDefaultContactInfo'){
            if (Yii::app()->request->isAjaxRequest) {
                $userId = Yii::app()->user->id;
                $newDefaultId = $_POST["newDefaultId"];
                $apiurl = "/LdAccountServer/order/". $mod;
                $params = array(
                    'SetDefaultContactInfoReq' => array(
                        'userId' => $userId,
                        'newDefaultId' => $newDefaultId
                    )
                );
                $row = $api->apiclient($apiurl, $params);
            }
        }

        $this->renderPartial('ajax', array('row' => $row, 'status' => $status));
    }

}

?>